// 专门处理权限路由的模块
import { constantRoutes, asyncRoutes } from '@/router' // 导入所有静态路由 和动态路由

const state = {
  routes: constantRoutes // 当前用户所拥有的路由数组  用于显示左侧菜单
}

const mutations = {
  SET_ROUTES(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}

const actions = {
  filterRoutes(context, menus) {
    const routes = []
    menus.forEach((key) => {
      routes.push(...asyncRoutes.filter((item) => item.name === key))
    })
    context.commit('SET_ROUTES', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
